//
//  ProgressImageView.swift
//  MicroBlog
//
//  Created by  NorthCity on 15/11/19.
//  Copyright © 2015年  NorthCity. All rights reserved.
//

import UIKit

class ProgressImageView: UIImageView {
    var progress:CGFloat = 0 {
        didSet{
            progressView.progress = progress
        }
    }
    
    //    MARK: - 构造函数
    init(){
        super.init(frame: CGRectZero)
        addSubview(progressView)
        progressView.backgroundColor = UIColor.clearColor()
        progressView.snp_makeConstraints { (make) -> Void in
        make.edges.equalTo(self.snp_edges)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    MARK: - 懒加载控件
    private lazy var progressView:ProgressView = ProgressView()
}

private class ProgressView:UIView{
    var progress:CGFloat = 0{
        didSet{
            setNeedsDisplay()
        }
    }
    
    override func drawRect(rect: CGRect) {

       let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
       let r = min(rect.width, rect.height) * 0.5
       let start = -CGFloat(M_PI_2)
       let end = start + progress * 2 * CGFloat(M_PI)
        
       let path = UIBezierPath(arcCenter: center, radius: r, startAngle: start, endAngle: end, clockwise:true)
        path.addLineToPoint(center)
       path.closePath()
       UIColor(white: 1, alpha: 0.7).setFill()
       path.fill()
    }

}
